package com.expmanage.actions.manager;

import java.math.*;
import java.util.*;

import org.apache.ibatis.session.*;

import com.expmanage.models.*;
import com.opensymphony.xwork2.*;
import static com.expmanage.actions.utils.DatabaseConfigure.*;

@SuppressWarnings("serial")
public class sRegist extends ActionSupport{
	private String schoolid, name, idnumber, 
		email, password, password_confirm, image;
	
	public String getSchoolid() {
		return schoolid;
	}

	public void setSchoolid(String schoolid) {
		this.schoolid = schoolid;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getIdnumber() {
		return idnumber;
	}

	public void setIdnumber(String idnumber) {
		this.idnumber = idnumber;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getPassword_confirm() {
		return password_confirm;
	}

	public void setPassword_confirm(String password_confirm) {
		this.password_confirm = password_confirm;
	}

	public String getImage() {
		return image;
	}

	public void setImage(String image) {
		this.image = image;
	}
	
	private String Register() {
		ActionContext context = ActionContext.getContext();
		Map<String, Object> pagesession = context.getSession();
		String rand = (String) pagesession.get("rand");
		if(!this.image.equals(rand)){
			return "failed";
		}
		
		Student newStudent = new Student(name, new BigDecimal(schoolid),
				new BigDecimal(idnumber) , password, email);
		SqlSession session = sqlSessionFactory.openSession();
		try {
			StudentMapper mapper = session.getMapper(StudentMapper.class);
			mapper.addStudent(newStudent);
			session.commit();
		} catch(Throwable e){
			e.printStackTrace();
			return "error";
		}finally {
			session.close();
		}
		return "success";
	}
	
	public String execute() {
		return Register();
	}
}
